<template>
	<view>
		<!-- 轮播图 -->
		<view class="swiper-container">
			<uni-swiper-dot class="uni-swiper-dot-box" :info="data" :current="current" field="content">
				<swiper class="swiper-box" :current="swiperDotIndex" @change="changeSwiper">
					<swiper-item v-for="(item, index) in data" :key="index">
						<view class="swiper-item" @click="clickBannerItem(item)">
							<image :src=" imagePrefix + item.image" mode="aspectFill" :draggable="false" />
						</view>
					</swiper-item>
				</swiper>
			</uni-swiper-dot>
		</view>
		<!-- 菜品选择区 -->
		<view class="content" style="margin-top: 8px;">
			<!-- 左侧分类栏 -->
			<scroll-view class="menu-wrapper" scroll-y :scroll-top="left_scroll" scroll-with-animation="true">
				<view class="left-content">
					<view style="position: relative;" v-for="(item,index) in goods" :key="index" ref="menuList"
						@click="select(index)" :class="{'current': currentIndex == index}">
						<view class="menu-item">{{item.name}}</view>
						<text class="allcount" v-if="getAllCount>=item.count&&item.count>0">{{item.count}}</text>
					</view>
				</view>
			</scroll-view>
			<!-- 右侧滚动 -->
			<scroll-view class="foods-wrapper" scroll-y :style="'height:'+windows_height+'px'" :scroll-top="foodSTop"
				@scroll="myscroll" scroll-with-animation="true">
				<view ref="foodsWrapper">
					<view ref="goods" class="foods" v-for="(item, i) in goods" :key="i">
						<view class="food-title" style="background: #f3f5f7">
							{{item.name}}
						</view>
						<view class="food" v-for="(food, index) in item.foods" :key="index" >
							<image :src="imagePrefix+ processlImagr(food.image)" mode="" @click="dishDetail(food.id)" style="width: 150rpx;;height: 150rpx;margin-top: 12rpx;border-radius: 10rpx;"></image>
							<view class="food-info">
								<text style="font-size: 28rpx;margin-top: 4rpx; ">{{food.name}}</text>
								<text style="font-size: 24rpx;color: #909090; margin: 4rpx 0; display: -webkit-box; -webkit-line-clamp: 3;  -webkit-box-orient: vertical;  overflow: hidden; text-overflow: ellipsis;">
								  {{food.description}}
								</text>
								<!-- <text style="font-size: 13px;margin: 2rpx 0 4rpx;">月售{{food.sellCount}}</text> -->
								<!-- 加减 -->
								<view class="food-btm">
									<text class="food-price">￥{{food.price}}</text>
									<cartcontrol :food="food" @add="addCart" @dec="decreaseCart"></cartcontrol>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view style="height: 150rpx; text-align: center;"> 已经到达底部</view>
			</scroll-view>
			<shopcart :goods="goods" @add="addCart" @dec="decreaseCart" @delAll="delAll"></shopcart>
		</view>

	</view>
</template>

<script>
	import shopcart from '@/components/shopcart.vue';
	import cartcontrol from '@/components/cartcontrol.vue'
	import Vue from 'vue';
	import {getDishList,getBanners} from '@/api/system/index.js'

	export default {
		data() {
			return {
				imagePrefix: this.$imagePrefix,
				title: 'Hello',
				current: 0,
				swiperDotIndex: 0,
				//轮播图数据
				data: [],
				//菜品数据
				goods: [],

				windows_height: 0, //屏幕高度
				foodSTop: 0, //右侧的滑动值
				currentIndex: 0,

				last: 0,
				right_height: [], //右侧每个内容的高度集合
				select_index: 0,
				left_height: 0, //左侧总高度
				left_scroll: 0, //左侧滑动值
				catrgoryList: [],
				timeout: null,

			}

		},
		components: {
			shopcart,
			cartcontrol
		},
		onLoad() {
			this.getData(); //轮播图列表
			this.getDish(); //菜品列表
			this.windows_height = Number(uni.getSystemInfoSync().windowHeight) - 55;
			setTimeout(() => {
				this.getHeightList();
			}, 1000)
		},
		//计算
		computed: {
			getList() {
				let result = [];
				this.goods.forEach((good) => {
					good.foods.forEach((food) => {
						if (food.count) {
							result.push(food)
						}
					})
				})
				return result

			},
			// 获得购物车所有商品数量
			getAllCount: function(item) {
				let result = [];
				let count = 0;

				for (let i = 0; i < this.goods.length; i++) {
					count = 0;
					this.goods[i].foods.forEach((food) => {
						if (food.count >= 0) {
							count += food.count
							Vue.set(this.goods[i], 'count', count)
						}
					})
					result.push(count)
				}

				result.sort(function(a, b) {
					return a - b;
				})
				count = result[result.length - 1]
				return count;
			},

		},
		// 自定义方法
		methods: {
			//图片处理显示一张
			processlImagr(url){
				const arr = url.split(',');
				return arr[0]
			},

			//获取轮播图列表
			getData() {
				// console.log("准备调轮播图接口");
				getBanners().then(res => {
					// console.log(res);
					this.data = res.data
				})
			},
			//轮播图
			changeSwiper(e) {
				this.current = e.detail.current
			},
			// 点击侧边栏
			select(index) {
				this.currentIndex = index;
				this.setScrollH(index);
			},

			// 设置点击侧边栏右边滚动的高度
			setScrollH: function(index) {
				var that = this;
				let height = 0;
				var query = uni.createSelectorQuery();
				let foods = query.selectAll('.foods');

				this.$nextTick(function() {
					foods.fields({
						size: true
					}, data => {
						if (index == 0) {
							that.foodSTop = 0;
						}
						for (let i = 0; i < index; i++) {
							height += parseInt(data[i].height);
							// console.log('fh', foods);
							that.foodSTop = height;
							// console.log(that.foodSTop)
						}
					}).exec();
				})

			},

			addCart: function(item) {
				if (item.count >= 0) {
					item.count++
					this.goods.forEach((good) => {
						good.foods.forEach((food) => {
							// 根据名字添加购物车,实际环境根据需要更改
							if (item.name == food.name)
								food.count = item.count
						})
					})
				} else {
					console.log('add')
					this.goods.forEach((good) => {
						good.foods.forEach((food) => {
							if (item.name == food.name)
								Vue.set(food, 'count', 1)
						})
					})
				}
			},
			decreaseCart(item) {
				if (item.count) {
					item.count--
					this.goods.forEach((good) => {
						good.foods.forEach((food) => {
							if (item.name == food.name)
								food.count = item.count
						})
					})
				}
			},
			// 清空购物车
			delAll() {
				this.goods.forEach((good) => {
					good.foods.forEach((food) => {
						if (food.count) {
							food.count = 0
						}
					})
				})
			},
			getHeightList() {
				let _this = this;
				let selectorQuery = uni.createSelectorQuery().in(this);
				selectorQuery.select('.left-content').boundingClientRect(function(rects) {
					_this.left_height = rects.height;
				});
				selectorQuery.selectAll('.foods').boundingClientRect(function(rects) {
					_this.right_height = rects.map((item) => item.top);
				}).exec();
			},
			myscroll(e) {
				//引入节流
				let right_content_height = e.detail.scrollHeight - this.windows_height;
				if (right_content_height == e.detail.scrollTop) {
					return;
				}
				let scroll_top = e.detail.scrollTop + 110; //110是banner图的高度
				//判断当前的scrollTop在哪个区间内;
				let now = +new Date();
				if (now - this.last > 100) {
					this.last = now;
					let active_index = this.right_height.findIndex((value, index, arr) => value <= scroll_top &&
						scroll_top < arr[index + 1]);
					this.currentIndex = active_index;
					if (this.left_height - this.windows_height) {
						//如果有超出部分
						let diff = this.left_height - this.windows_height
						this.left_scroll = Math.round((active_index * diff) / (this.goods.length - 1))
					}
				}
			},
			//获取菜单列表
			getDish() {
				// console.log("准备调菜品接口");
				getDishList().then(res => {
					console.log(res.data);
					this.goods = res.data
				})
			},
			//菜品详情信息
			dishDetail(id){
				this.$tab.navigateTo('/pages/dishDetail/dishDetail?id=' + id);
			}
		}
	}
</script>

<style>
	.swiper {
		height: 300rpx;
	}

	.swiper-box {
		height: 300rpx;
	}

	.swiper-item {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		color: #fff;
		height: 300rpx;
		line-height: 300rpx;
	}

	@media screen and (min-width: 1000rpx) {
		.uni-swiper-dot-box {
			width: 800rpx;
			/* #ifndef APP-NVUE */
			margin: 0 auto;
			/* #endif */
			margin-top: 80rpx;
		}

		.image {
			width: 100%;
		}
	}

	.content {
		display: flex;
		top: 0;
		bottom: 110rpx;
		width: 100%;
		overflow: hidden;
	}

	.current {
		position: relative;
		z-index: 0;
		background-color: #fff;
		color: #00A0DC;
		border-left: 10rpx solid #00A0DC;
	}

	.menu-wrapper {
		text-align: center;
		width: 22%;
		display: flex;
		flex-direction: column;
		background: #f3f5f7;

	}

	.menu-item {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		height: 100rpx;
	}

	.allcount {
		position: absolute;
		right: 12rpx;
		top: 16rpx;
		display: inline-block;
		padding: 0 8rpx;
		font-size: 18rpx;
		line-height: 32rpx;
		font-weight: 400;
		border-radius: 50%;
		background-color: #f01414;
		color: #ffffff;
	}

	.foods-wrapper {
		margin-left: 8rpx;
		width: 78%;
	}

	.food,
	.food-btm,
	.content {
		display: flex;
		flex-direction: row;
	}

	.food-title {
		padding: 4rpx 0;
	}

	.food-info {
		margin-left: 20rpx;
		margin-right: 32rpx;
		display: flex;
		flex-direction: column;
		flex: 2;
	}

	.food-btm {
		justify-content: space-between;
	}

	.food-price {
		color: #f01414;
		font-size: 30rpx;
	}
</style>
